﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Channels;
using OneWaySoapChannelLib.Channel;

namespace OneWaySoapChannelLib.Binding
{
    public class OneWaySoapBindingElement : TransportBindingElement
    {
        public OneWaySoapBindingElement() : base()
        {
        }

        public OneWaySoapBindingElement(OneWaySoapBindingElement elementToBeCloned)
            : base(elementToBeCloned)
        {
        }
        
        public string ConnectionString { get; set; }

        public override string Scheme
        {
            get { return "net.null"; }
        }

        public override BindingElement Clone()
        {
            return new OneWaySoapBindingElement(this);
        }

        public override T GetProperty<T>(BindingContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            return base.GetProperty<T>(context);
        }

        public override bool CanBuildChannelFactory<TChannel>(BindingContext context)
        {
            if (typeof(TChannel) == typeof(IOutputChannel)) return true;
            return false;
        }

        public override bool CanBuildChannelListener<TChannel>(BindingContext context)
        {
            return false;
        }

        public override IChannelFactory<TChannel> BuildChannelFactory<TChannel>(BindingContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            if (!this.CanBuildChannelFactory<TChannel>(context))
            {
                throw new InvalidOperationException(string.Format("ChannelTypeNotSupported - {0}", typeof(TChannel).Name));
            }
            if (base.ManualAddressing)
            {
                throw new InvalidOperationException("ManualAddressingNotSupported");
            }
            return new SoapStoreChannelFactory<TChannel>(this, context);
        }

        public override IChannelListener<TChannel> BuildChannelListener<TChannel>(BindingContext context)
        {
            throw new ArgumentNullException("channel listener not implemented");
        }

    }
}
